-- General
function ccR_Shaman_Lightning_Shield()
	if ccrPlayerHasSpell(LIGHTNING_SHIELD)
	and not ccrPlayerHasBuff(LIGHTNING_SHIELD)
	and ccrSpellIsReady(LIGHTNING_SHIELD)
	then
		ccrCast(LIGHTNING_SHIELD)
		return
	end
	return
end
function ccR_Shaman_Earth_Shock()
	if ccrPlayerHasSpell(EARTH_SHOCK)
	and ccrSpellCanAttack(EARTH_SHOCK)
	and ccrPlayerIsFacingTarget()
	then
		ccrCastAttack(EARTH_SHOCK)
		return
	end
	return
end
function ccR_Shaman_Flame_Shock()
	if ccrPlayerHasSpell(FLAME_SHOCK)
	and ccrSpellCanAttack(FLAME_SHOCK)
	and ccrPlayerIsFacingTarget()
	then
		ccrCastAttack(FLAME_SHOCK)
		return
	end
	return
end
function ccR_Shaman_Frost_Shock()
	if ccrPlayerHasSpell(FROST_SHOCK)
	and ccrSpellCanAttack(FROST_SHOCK)
	and ccrPlayerIsFacingTarget()
	then
		ccrCastAttack(FROST_SHOCK)
		return
	end
	return
end
function ccR_Shaman_Purge()
end
function ccR_Shaman_Lightning_Bolt()
	if ccrPlayerHasSpell(LIGHTNING_BOLT)
	and ccrSpellCanAttack(LIGHTNING_BOLT)
	and ccrPlayerIsFacingTarget()
	then
		ccrCastAttack(LIGHTNING_BOLT)
		return
	end
end
function ccR_Shaman_Chain_Lightning()
	if ccrPlayerHasSpell(CHAIN_LIGHTNING)
	and ccrSpellCanAttack(CHAIN_LIGHTNING)
	and ccrPlayerIsFacingTarget()
	then
		ccrCastAttack(CHAIN_LIGHTNING)
		return
	end
end
function ccR_Shaman_Stormlash_Totem()
	if ccrPlayerHasSpell(STORMLASH_TOTEM)
	and ccrSpellIsReady(STORMLASH_TOTEM)
	and (ccrTargetIsBoss() or ccrTargetIsRare() or ccrTargetIsEnemyPlayer())
	then
		ccrCast(STORMLASH_TOTEM)
		return
	end
end
function ccR_Shaman_Ascendance()
	if ccrPlayerHasSpell(ASCENDANCE)
	and ccrSpellIsReady(ASCENDANCE)
	and not ccrPlayerHasBuff(ASCENDANCE)
	and ccrPlayerIsInCombat()
	then
		ccrCast(ASCENDANCE)
		return
	end
	return
end
vLast_Wind_Shear_Anyone_Check = 0.1
function ccR_Shaman_Wind_Shear()
	local vInteruptAnyone = true
	local vInteruptTarget = true
	if vInteruptAnyone
	and (GetTime()-vLast_Wind_Shear_Anyone_Check) > 0.10
	then
		if # tblPGetNearbyEnemies > 0 then
			for x = 0, # tblPGetNearbyEnemies do
				local obj = tblPGetNearbyEnemies[x]
				if obj
				and obj:Exists()
				and (obj:GetTarget():Exists() and obj:GetTarget():GetName() == ccrPlayerName())
				and obj:InLineOfSight()
				and ccrUnitIsCastingOrChanneling(obj)
				and Player:IsFacing(obj)
				and ccrSpellCanAttack(WIND_SHEAR, obj)
				then
					local vCounterSpell = GetSpellInfo(WIND_SHEAR)
					if ccrUnitIsCasting(obj)
					and ccrUnitHasLeftOnCast(obj, 1)
					then
						obj:CastSpellByName(vCounterSpell)
						return
					end
					if ccrUnitIsChanneling(obj) then
						obj:CastSpellByName(vCounterSpell)
						return
					end									
				end
			end
		end	
		vLast_Wind_Shear_Anyone_Check = GetTime()
	end
	if vInteruptTarget then
		if ccrPlayerHasSpell(WIND_SHEAR)
		and ccrSpellIsReady(WIND_SHEAR)
		and ccrTargetIsCastingOrChanneling()
		and ccrTargetIsInteruptable()
		and ccrPlayerIsFacingTarget()
		then
			if ccrTargetIsCasting()
			and ccrTargetHasLeftOnCast(1)
			then
				ccrCastAttack(WIND_SHEAR)
				return
			end
			if ccrTargetIsChanneling() then
				ccrCastAttack(WIND_SHEAR)
				return
			end
		end
	end
end
function ccR_Shaman_Shamanistic_Rage()
	if ccrPlayerIsInCombat()
	and ccrPlayerHasSpell(SHAMANISTIC_RAGE)
	and ccrSpellIsReady(SHAMANISTIC_RAGE)
	and ccrPlayerHealth() <= 60
	then
		ccrCast(SHAMANISTIC_RAGE)
		return
	end
	return
end
function ccR_Shaman_Tremor_Totem()
	if ccrPlayerHasSpell(TREMOR_TOTEM)
	and ccrSpellIsReady(TREMOR_TOTEM)
	then
		local eventIndex = C_LossOfControl.GetNumEvents()
		while (eventIndex > 0) do
			local _, _, text = C_LossOfControl.GetEventInfo(eventIndex)
			if text == LOSS_OF_CONTROL_DISPLAY_CHARM
			or text == LOSS_OF_CONTROL_DISPLAY_SLEEP
			or text == LOSS_OF_CONTROL_DISPLAY_FEAR
			or text == LOSS_OF_CONTROL_DISPLAY_HORROR
			then
				ccrCast(TREMOR_TOTEM)
				return
			end
			eventIndex = eventIndex - 1
		end
	end
end
function ccR_Shaman_Healing_Stream_Totem()
	if ccrPlayerHasSpell(HEALING_STREAM_TOTEM)
	and ccrSpellIsReady(HEALING_STREAM_TOTEM)
	and ccrPlayerIsInCombat()
	and ccrPlayerHealth() <= 60
	then
		ccrCast(HEALING_STREAM_TOTEM)
		return
	end
end
function ccR_Shaman_Healing_Tide_Totem()
	if ccrPlayerHasSpell(HEALING_TIDE_TOTEM)
	and ccrSpellIsReady(HEALING_TIDE_TOTEM)
	and ccrPlayerIsInCombat()
	and ccrPlayerHealth() <= 40
	then
		ccrCast(HEALING_TIDE_TOTEM)
		return
	end
end
vLast_Grounding_Totem_Check = 0.1
function ccR_Shaman_Grounding_Totem()
	if ccrPlayerHasSpell(GROUNDING_TOTEM)
	and ccrSpellIsReady(GROUNDING_TOTEM)
	and (GetTime()-vLast_Grounding_Totem_Check) > 0.10
	then
		local x
		if # tblPGetNearbyEnemies > 0 then
			for x = 0, # tblPGetNearbyEnemies do
				local obj = tblPGetNearbyEnemies[x]
				if obj
				and obj:Exists()
				and (obj:GetTarget():Exists() and obj:GetTarget():GetName() == ccrPlayerName())
				and ccrUnitIsCastingOrChanneling(obj)
				then
					ccrCast(GROUNDING_TOTEM)
					vLast_Grounding_Totem_Check = GetTime()
					return
				end
			end
		end	
		vLast_Grounding_Totem_Check = GetTime()
	end
end
-- Elemental

-- Enhancement
function ccR_Shaman_Stormstrike()
	if ccrPlayerHasSpell(STORMSTRIKE)
	and ccrSpellCanAttack(STORMSTRIKE)
	and ccrPlayerIsFacingTarget()
	then
		ccrCastAttack(STORMSTRIKE)
		return
	end
	if not ccrPlayerHasSpell(STORMSTRIKE)
	and ccrPlayerHasSpell(PRIMAL_STRIKE)
	and ccrSpellCanAttack(PRIMAL_STRIKE)
	and ccrPlayerIsFacingTarget()
	then
		ccrCastAttack(PRIMAL_STRIKE)
		return
	end
	return
end
function ccR_Shaman_Lavalash()
	if ccrPlayerHasSpell(LAVA_LASH)
	and ccrSpellCanAttack(LAVA_LASH)
	and ccrPlayerIsFacingTarget()
	then
		ccrCastAttack(LAVA_LASH)
		return
	end
	return
end
vLast_Lavalash_Spread_Check = 0.1
function ccR_Shaman_Lavalash_Spread_Flame_Shock()
	if ccrPlayerHasSpell(LAVA_LASH)
	and ccrSpellCanAttack(LAVA_LASH)
	and #tblPGetMeleeEnemies >= 2
	and (GetTime()-vLast_Lavalash_Spread_Check) > 0.10
	then
		for x = 1, #tblPGetMeleeEnemies do
			local obj = tblPGetMeleeEnemies[x]
			if obj
			and obj:Exists()
			and Player:IsFacing(obj)
			and ccrUnitHasMyDebuff(obj, FLAME_SHOCK) 
			then
				ccrCastAttack(LAVA_LASH, obj)
				return
			end
		end
		vLast_Lavalash_Spread_Check = GetTime()
	end
end
function ccR_Shaman_Shock()
	-- PVP/Frost Shock
	if ccrTargetIsEnemyPlayer()
	and ccrPlayerHasSpell(FROST_SHOCK)
	and not ccrTargetHasMyDebuff(FROST_SHOCK)
	then
		ccR_Shaman_Frost_Shock()
	end
	-- Flame Shock > Earth Shock
	if not ccrPlayerHasSpell(FLAME_SHOCK) then
		ccR_Shaman_Earth_Shock()
	else
		if not ccrTargetHasMyDebuff(FLAME_SHOCK) then
			ccR_Shaman_Flame_Shock()
		else
			ccR_Shaman_Earth_Shock()
		end
	end
end
vLast_Fire_Nova_Check = 0.1
function ccR_Shaman_Fire_Nova()
	if ccrPlayerHasSpell(FIRE_NOVA)
	and ccrSpellIsReady(FIRE_NOVA)
	and #tblPGetNearbyEnemies >= 1
	and (GetTime()-vLast_Fire_Nova_Check) >= 0.10
	then
		for x = 1, #tblPGetNearbyEnemies do
			local obj = tblPGetNearbyEnemies[x]
			if obj
			and obj:Exists()
			and ccrUnitHasMyDebuff(obj, FLAME_SHOCK) 
			and # obj:GetNearbyEnemies(12) >= 2
			then
				ccrCast(FIRE_NOVA)
				vLast_Fire_Nova_Check = GetTime()
				return
			end
		end
		vLast_Fire_Nova_Check = GetTime()
	end
end
function ccR_Shaman_Maelstrom_Weapon()
	if ccrPlayerHasBuffStacks(MAELSTROM_WEAPON, 5) then
		if ccrPlayerHealth() <= 80 then
			ccrCast(HEALING_SURGE, "player")
		else
			local vCount = ccrTargetCount(10)
			if vCount == 1 then
				ccR_Shaman_Lightning_Bolt()
			end
			if vCount >= 2 then
				ccR_Shaman_Chain_Lightning()
			end
		end
	end
end
function ccR_Shaman_Feral_Spirit()
	if ccrPlayerHasSpell(FERAL_SPIRIT)
	and ccrSpellIsReady(FERAL_SPIRIT)
	and (ccrTargetIsBoss() or ccrTargetIsRare() or ccrTargetIsEnemyPlayer())
	then
		ccrCast(FERAL_SPIRIT)
		return
	end
end
vLast_Flame_Shock_All = 0.1
function ccR_Shaman_Enhancement_Flame_Shock_All()
	if ccrPlayerHasSpell(FLAME_SHOCK)
	and ccrSpellCanAttack(FLAME_SHOCK)
	and ccrTargetHasMyDebuff(FLAME_SHOCK)
	and #tblPGetNearbyEnemies >= 2
	and (GetTime()-vLast_Flame_Shock_All) > 0.10
	then
		for x = 1, #tblPGetNearbyEnemies do
			local obj = tblPGetNearbyEnemies[x]
			if obj
			and obj:Exists()
			and Player:IsFacing(obj)
			and (obj:IsTappedByPlayer() or (obj:GetTarget():Exists() and obj:GetTarget():GetName() == ccrPlayerName()))			
			and not ccrUnitHasMyDebuff(obj, FLAME_SHOCK)
			then
				ccrCastAttack(FLAME_SHOCK, obj)
				vLast_Fire_Nova_Check = GetTime()
				return
			end
		end
		vLast_Flame_Shock_All = GetTime()
	end
	return
end
function ccR_Shaman_Unleash_Elements()
	if ccrPlayerHasSpell(UNLEASH_ELEMENTS)
	and ccrSpellCanAttack(UNLEASH_ELEMENTS)
	then
		ccrCastAttack(UNLEASH_ELEMENTS)
		return
	end
	return
end

-- Restoration
